/**
 * 
 */
package com.objectdraw.client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.objectdraw.client.objectdraw.*;

/**
 * This class is responsible for the starting of the client.  It creates the inital GUI and calls the 
 * clientcomm class to create a socket.  
 * 
 * @author Harrison
 * 
 */
public class CollabDraw {
	clientcomm com;

	/**
	 * Creates a frame and adds buttons to it.  Waits for user input.  Then it 
	 *  adds and ObjectDraw3 instance to the GUI.
	 */
	public CollabDraw(){
		JFrame frame = new JFrame();
		frame.setTitle("CollabDraw");
		frame.getContentPane().setLayout(new BorderLayout());
		//connectGui=new GuiServerConnect();
		String[] rofl=new String[]{"",""};
		while(com==null){
			MessageBox lol=new MessageBox();
			rofl = lol.doInputBox("connect", "IP", rofl[0],"Name",rofl[1]);
			if(lol.actionCode==lol.CANCEL_ACTION){System.exit(0);}
			try{com=new clientcomm(rofl[0]);}
			catch(Exception e){
				System.out.println(e.toString());
			}
			if(rofl[1].length()==0){
				System.out.println("name required/too short");
				com.close();
				com=null;
			}
		}
		com.sendMessage("connect " +rofl[1]);
		com.sendMessage("Control request");
		ObjectDraw3 objd3 = new ObjectDraw3(com);
		frame.getContentPane().add(objd3);
		frame.addWindowListener(new AppCloser());
		frame.pack();
		frame.setSize(800,600);
		frame.setVisible(true);
		new EchoThread(com,objd3.canvas).start();
	}


	/**
	 * Runs on starting of JVM.  Creates new instance of the client.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new CollabDraw();
	}

	/* Inner class AppCLoser for terminating application */
	/* when Close Window button of frame is clicked */

	class AppCloser extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			com.close();
			System.exit(0);
		}
	}

	class MessageBox {

		public final int NO_ACTION = -1;
		public final int CANCEL_ACTION = 0;
		public final int OK_ACTION = 1;
		int actionCode = NO_ACTION;

		Frame parent;
		Dialog msgbox;
		Panel buttons;
		Button ok, cancel;
		TextField textbox;
		TextField textbox2;

		public MessageBox ()  {
			// this is the invisible Frame we'll be using to call all of our Dialogs
			parent = new Frame();
		}

		public void dispose ()  {
			// use this when you're ready to clean up
			try { msgbox.dispose(); }  catch (Exception e)  {}
			try { parent.dispose(); }  catch (Exception e)  {}
		}

		public String[] doInputBox (String title, String message, String defaultText, String message2, String defaultText2)  {
			actionCode = NO_ACTION;
			try  {
				// create the messagebox
				msgbox = new Dialog(parent, title, true);
				msgbox.setLayout(new GridLayout(3,1));

				FlowLayout flow1 = new FlowLayout();
				JPanel flow1panel =new JPanel();
				flow1panel.setLayout(flow1);
				Label msglabel = new Label(message);
				flow1panel.add(msglabel);
				// create the text field
				textbox = new TextField(defaultText, 40);
				// the ActionListener here will get called if the user presses "Enter"
				textbox.addActionListener( new ActionListener()  {
					public void actionPerformed(ActionEvent e)  {
						actionCode = OK_ACTION;
						msgbox.dispose();
					}
				} );
				flow1panel.add(textbox);
				msgbox.add(flow1panel);


				FlowLayout flow2 = new FlowLayout();
				JPanel flow2panel =new JPanel();
				flow1panel.setLayout(flow2);
				Label msglabel2 = new Label(message2);
				flow2panel.add(msglabel2);
				// create the text field
				textbox2 = new TextField(defaultText2, 40);
				// the ActionListener here will get called if the user presses "Enter"
				textbox2.addActionListener( new ActionListener()  {
					public void actionPerformed(ActionEvent e)  {
						actionCode = OK_ACTION;
						msgbox.dispose();
					}
				} );
				flow2panel.add(textbox2);
				msgbox.add(flow2panel);

				// create the OK and Cancel buttons
				buttons = new Panel();
				buttons.setLayout(new FlowLayout());
				initOkButton();
				buttons.add(ok);
				initCancelButton();
				buttons.add(cancel);
				msgbox.add(buttons);

				// display everything (the system will wait until a button is pressed
				// before returning)
				displayMsgbox();
				msgbox.dispose();		// just in case the ActionListeners didn't fire...
			}  catch (Exception e)  {
			}

			if (actionCode == OK_ACTION)
				return new String[]{textbox.getText(),textbox2.getText()};
			else
				return null;
		}

		private void initOkButton ()  {
			// the OK button
			ok = new Button("OK");
			ok.addActionListener( new ActionListener()  {
				public void actionPerformed(ActionEvent e)  {
					actionCode = OK_ACTION;
					msgbox.dispose();
				}
			} );
		}

		private void initCancelButton ()  {
			// the Cancel button
			cancel = new Button("Cancel");
			cancel.addActionListener( new ActionListener()  {
				public void actionPerformed(ActionEvent e)  {
					actionCode = CANCEL_ACTION;
					msgbox.dispose();
				}
			} );
		}


		private void displayMsgbox ()  {
			// once all of the components have been added to a dialog,
			// use this method to display it
			Dimension d = msgbox.getToolkit().getScreenSize();
			msgbox.setLocation(d.width/3, d.height/3);		// center the dialog (sort of)
			msgbox.pack();						// organize all its components
			msgbox.setResizable(false);		// make sure the user can't resize it
			msgbox.toFront();					// give it focus
			msgbox.setVisible(true);					// and display it
		}
	}
}
